<script type="text/javascript">
function memProfile(member)
{
	window.location='index.php?viewwhole='+member;
}

function funDelMem(userId) {
	var delPro=confirm("Are you sure want to delete wholeseller?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidDelMember").value=userId;
	    frm.submit();
	}	
}

function funAppMem(userId) {
	var delPro=confirm("Are you sure want to allow this wholeseller?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidApprove").value=userId;
	    frm.submit();
	}	
}


function funDAppMem(userId) {
	var delPro=confirm("Are you sure you want to deny this wholeseller?");
	if(delPro==true){
		var frm = document.getElementById('form1');
		document.getElementById("hidDeny").value=userId;
		frm.submit();
	}
}

function funDisAppMem(userId) {
	var delPro=confirm("Are you sure want to block wholeseller?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidDisApprove").value=userId;
	    frm.submit();
	}	
}

</script>
<?php

$cond = '';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
	
	if($_GET['sort']=='user_type')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY user_type ASC';	$sort = '&sort=user_type&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' ORDER BY user_type DESC';	$sort = '&sort=user_type&s_type=desc';	}
	}
	else if($_GET['sort']=='username')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY username ASC';		$sort = '&sort=username&s_type=asc';	$u='desc';	}
		else
		{	$cond=' ORDER BY username DESC';	$sort = '&sort=username&s_type=desc';	}
	}
	else if($_GET['sort']=='first_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY first_name ASC';	$sort = '&sort=first_name&s_type=asc';	$fn='desc';	}
		else
		{	$cond=' ORDER BY first_name DESC';	$sort = '&sort=first_name&s_type=desc';	}
	}
	else if($_GET['sort']=='last_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY last_name ASC';	$sort = '&sort=last_name&s_type=asc';	$ln='desc';	}
		else
		{	$cond=' ORDER BY last_name DESC';	$sort = '&sort=last_name&s_type=desc';	}
	}
	else if($_GET['sort']=='created_date')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY created_date ASC';	$sort = '&sort=created_date&s_type=asc'; $dg='desc'; }
		else
		{	$cond=' ORDER BY created_date DESC';	$sort = '&sort=created_date&s_type=desc';}
	}
}
if(!empty($_POST['hidDeny']))
{
	$mId=$_POST['hidDeny'];
	$sqlAppMem="Update tbl_users set denyStatus=1 where user_id='$mId'";
	$rstAppMem=mysql_query($sqlAppMem);
}
if(!empty($_POST['hidApprove']))
{
	$mId=$_POST['hidApprove'];
	$sqlAppMem="Update tbl_users set status=1,denyStatus=0 where user_id='$mId'";
	$rstAppMem=mysql_query($sqlAppMem);	
	
	$usrInfo=mysql_fetch_array(mysql_query("select * from tbl_users where user_id='$mId'"));
	
	$sqlEmail="select * from tbl_emails where email_type='Wholeseller Approval'";
	
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	 
	 //user email
	  $sqluseremail="SELECT * FROM tbl_users where user_id='$mId'";				 
	  $rstuseremail=mysql_query($sqluseremail);	
	  $rouseremail=mysql_fetch_array($rstuseremail);
	  $email=$rouseremail['email'];
	 //$emailContent=str_replace('##_MerChantUser',$rouseremail['username'],$rowEmail['email_body']);
	  $emailContent=str_replace('%fname%',stripslashes($usrInfo['first_name']),str_replace('%lname%',stripslashes($usrInfo['last_name']),str_replace('%username%', stripslashes($usrInfo['username']), $rowEmail['email_body'])));

	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
</table>
";

		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $firstName);
		$mail->Send();
	
}
if(!empty($_POST['hidDelMember']))
{
	$memberId=$_POST['hidDelMember'];
	$sqlDelMem="Delete from tbl_users where user_id=$memberId";
	$rstDelMem=mysql_query($sqlDelMem);	
}
?>
<form name="form1" id="form1" method="post" action="">
			  <input type="hidden" name="hidDelMember" id="hidDelMember">
			  <input type="hidden" name="hidApprove" id="hidApprove">
			  <input type="hidden" name="hidDisApprove" id="hidDisApprove">
				<input type="hidden" name="hidDeny" id="hidDeny" />
			  
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
	<td width="469" height="25" class="tdHeader">New Wholesellers  List</td>
  </tr>
  <tr>
    <td height="577">
	<table width="100%">
		<tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
						<td width="20%">Date Registered</td>
						<td width="30%">
							<input type="text" name="created_date" id="created_date" style="width: 150px;" value="<?php echo $_POST['created_date']?>" />
						</td>
						<td width="20%">Username</td>
						<td width="30%"><input type="text" name="username" id="username" style="width: 150px;" value="<?php echo $_POST['username']?>" /></td>
					</tr>
					<tr>
						<td width="20%">First Name</td>
						<td width="30%">
							<input type="text" name="first_name" id="first_name" style="width: 150px;" value="<?php echo $_POST['first_name']?>" />
						</td>
						<td width="20%">Last Name</td>
						<td width="30%"><input type="text" name="last_name" id="last_name" style="width: 150px;" value="<?php echo $_POST['last_name']?>" /></td>
					</tr>

					<tr><td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Member" /></td></tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<b>Sort  By :</b>
				<a href="<?php echo $base_url;?>admin/index.php?newmer&sort=user_type&s_type=<?php echo $ut;?>">Member Type</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?newmer&sort=username&s_type=<?php echo $u;?>">Username</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?newmer&sort=first_name&s_type=<?php echo $fn;?>">First name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?newmer&sort=last_name&s_type=<?php echo $ln;?>">Last name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?newmer&sort=created_date&s_type=<?php echo $dg;?>">Date Regd.</a>
			</td>
		</tr>
		</tbody>
	</table><br>
	<table width="100%"  border="0" cellspacing="0">      
      <tr class="InnerHeader">       
        <td width="14%" class="line">Member type </td>
	<td width="20%" class="line">Wholeseller Name </td>
        <td width="20%" class="line">First Name </td>
        <td width="15%" class="line">Last Name </td>
	<td width="7%" class="line">&nbsp;</td>
        <td width="8%" class="line">&nbsp;</td>
        <td width="7%" class="line">&nbsp;</td> 
	<td width="9%" class="line">&nbsp;</td>         
      </tr>     
      <?php 				 
	require_once "../includes/pagination.php";
	$itemPerPage=15;
	$search='';
	if(isset($_POST['btnSearchPro'])) {
		
		$username 	= $_POST['username'];		
		$created_date 	= $_POST['created_date'];
		$first_name 	= $_POST['first_name'];
		$last_name 	= $_POST['last_name'];
		
		if($username!='')
		$search.= " AND username='".$username."'";
		
		if($created_date!='')
		$search.=" AND created_date='".$created_date."'";
		
		if($first_name!='')
		$search.="AND first_name='".$first_name."'";
		
		if($last_name!='')
		$search.="AND last_name='".$last_name."'";
		
	}
	$sql_shocat="SELECT * FROM tbl_users where user_type=5 and status=2 and denyStatus=0 $search $cond";
	$rst_shocat=mysql_query(getPagingQuery($sql_shocat,$itemPerPage));			
	$numRows = mysql_num_rows($rst_shocat);
	if($numRows>0) {
		while($row_shocat = mysql_fetch_array($rst_shocat)) {				 
			$mem_id=$row_shocat['user_id'];
			$mem_name=$row_shocat['username'];
			$usr_type=$row_shocat['user_type'];				 
			$mem_fname=$row_shocat['first_name'];
			$mem_lname=$row_shocat['last_name'];
			echo "<tr>";
			echo "<td class='line'>Wholeseller</td>";
			echo "<td class='line'>$mem_name</td>";
			echo "<td class='line'>$mem_fname</td>";
			echo "<td class='line'>$mem_lname</td>";
			echo "<td height='20' class='line'><input type='button' name='btnDeny' value='Deny' class='button'  onclick='funDAppMem($mem_id)'></td> ";
			echo "<td height='20' class='line'><input type='button' name='btnApprove' value='Approve' class='button'  onclick='funAppMem($mem_id)'></td> ";
			echo "<td height='20' class='line'><input type='button' name='view' value='View' class='button'  onclick='memProfile($mem_id)'></td> ";
			echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='funDelMem($mem_id)';></td> ";
			echo "</tr>";
	       
		} 
	}
	else {
	       echo " <tr><td colspan='8' class='line'>No Wholeseller Found.</td></tr>";
	}				 
	?>      
       </table>
	<div align="center">
		<?php		   
		   $link="wholeseller";
		   echo $disp=getPagingLink($sql_shocat,$itemPerPage,$link);							   
		?>    
	</div>
	</td>
 </tr>
</table>